package com.example.oasystem.controller.plan;

import com.example.oasystem.config.ConstantProperties;
import com.example.oasystem.entity.attachment.AoaAttachmentListOfl;
import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.plan.AoaPlanList;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaPlanListService;
import com.example.oasystem.utils.AliyunOSSUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AoaPlanListController {

    @Autowired
    AoaPlanListService aoaPlanListService;
    @Autowired
    AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    ConstantProperties constantProperties;
    @RequestMapping("/planview")
    public String planview(Model model, HttpServletRequest request,
                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                           @RequestParam(value = "baseKey", defaultValue = "") String baseKey) {
        //存储类型表集合
        List<AoaTypeList> aoaTypeLists = aoaPlanListService.typeList();
        model.addAttribute("typelist", aoaTypeLists);
        System.out.println("baseKey = " + baseKey);
        //存储状态表类型
        List<AoaStatusList> aoaStatusLists = aoaPlanListService.statusList();
        model.addAttribute("statuslist", aoaStatusLists);

        //文件下载
        List<AoaAttachmentListOfl> attachment = aoaPlanListService.attachment();
        model.addAttribute("attachment",attachment);

        //取session里面的user
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        //查询部门
        Long deptId = user.getDeptId();
        AoaDept dept = aoaPlanListService.dept(deptId);
        model.addAttribute("dept", dept);

        //查询计划
        PageInfo<AoaPlanList> plans = aoaPlanListService.plans(userId, baseKey, pageNum, pageSize);
        Page page = new Page(plans.getSize(), Math.toIntExact(plans.getTotal()), plans.getPages(), plans.getPageNum(), plans.isIsFirstPage(), plans.isIsLastPage());
        model.addAttribute("page", page);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("plist", plans.getList());
        model.addAttribute("url", "planviewtable");
        return "/plan/planview";
    }

    @RequestMapping("/planviewtable")
    public String planviewtable(Model model, HttpServletRequest request,
                                @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                                @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                @RequestParam(value = "type", required = false) Integer type,
                                @RequestParam(value = "time", required = false) Integer time,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "icon", required = false, defaultValue = "") String icon) {
        System.out.println("baseKey = " + baseKey);
        System.out.println("type"+type+"time"+time+"status"+status);
        //存储类型表集合
        List<AoaTypeList> aoaTypeLists = aoaPlanListService.typeList();
        model.addAttribute("typelist", aoaTypeLists);
        //存储状态表类型
        List<AoaStatusList> aoaStatusLists = aoaPlanListService.statusList();
        model.addAttribute("statuslist", aoaStatusLists);

        //文件下载
        List<AoaAttachmentListOfl> attachment = aoaPlanListService.attachment();
        model.addAttribute("attachment",attachment);
        //取session里面的user
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        //查询部门
        Long deptId = user.getDeptId();
        AoaDept dept = aoaPlanListService.dept(deptId);
        model.addAttribute("dept", dept);

        //查询计划
        PageInfo<AoaPlanList> myplans = aoaPlanListService.myPlans(userId, baseKey,type,time,status,pageNum, pageSize);
        Page page = new Page(myplans.getSize(), Math.toIntExact(myplans.getTotal()), myplans.getPages(), myplans.getPageNum(), myplans.isIsFirstPage(), myplans.isIsLastPage());
        model.addAttribute("page", page);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("plist", myplans.getList());
        model.addAttribute("url", "planviewtable");
        model.addAttribute("type",type);
        model.addAttribute("status",status);
        model.addAttribute("time",time);
        model.addAttribute("icon",icon);
        return "/plan/planviewtable";
    }
//    @RequestMapping("down")
//    public String down(@RequestParam("pid")Long pid){
//
//    }
    @RequestMapping("/planedit")
    public String planedit(@RequestParam("pid")Long pid,Model model,HttpServletRequest request,
                           @RequestParam(value = "pageNum",required = false,defaultValue = "1")Long pageNum){
        String re="";
        //存储类型表集合
        List<AoaTypeList> aoaTypeLists = aoaPlanListService.typeList();
        model.addAttribute("typelist", aoaTypeLists);
        //存储状态表类型
        List<AoaStatusList> aoaStatusLists = aoaPlanListService.statusList();
        model.addAttribute("statuslist", aoaStatusLists);

        if (pid==-1){
        }
        if (pid!=-1){
            String baseKey = request.getParameter("baseKey");
            String num = request.getParameter("pageNum");
            System.out.println("numdddddddddddd = " + num);

            AoaPlanList aoaPlanList = aoaPlanListService.selectByPrimaryKey(pid);
            model.addAttribute("plan",aoaPlanList);
            model.addAttribute("pageNum",pageNum);
            model.addAttribute("baseKey",baseKey);
        }
        return "/plan/planedit";
    }
    @RequestMapping("plandelete")
    public String plandelete(@RequestParam("pid")Long pid,@RequestParam("pageNum")int pageNum){
        aoaPlanListService.deleteByPrimaryKey(pid);
        return "forward:/planview?pageNum="+pageNum;
    }
    @RequestMapping("/plansave")
    public String plansave(AoaPlanList aoaPlanList, HttpServletRequest request, MultipartFile filePath){
        String re="";
        if (aoaPlanList.getPlanId()==null)
        {
            //时间格式化
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String nowdatehms = sdf.format(date);  //获取当前系统时间
            aoaPlanList.setCreateTime(nowdatehms);
            //进行文件上传
            if (filePath!=null){
            String file = uploadFile(filePath);
            AoaAttachmentListOfl aoaAttachment = new AoaAttachmentListOfl();
            aoaAttachment.setAttachmentPath(file);
            aoaAttachment.setAttachmentName(file);
            int document = aoaPlanListService.insertSelectivePath(aoaAttachment);
            System.out.println("aoaAttachmentccccccccccccccc = " + aoaAttachment);
            Long attachId = aoaAttachment.getAttachmentId();
            aoaPlanList.setAttachId(attachId);
            }
            //进行计划添加
            int i = aoaPlanListService.insertSelective(aoaPlanList);
            re="forward:planview";
        }
        else
        {
            System.out.println("执行到update = " + aoaPlanList);
            String baseKey = request.getParameter("baseKey");

            String num = request.getParameter("pageNum");
            System.out.println("bbbbbbbbbbbbbbb = " + num);
            int pageNum = Integer.parseInt(num);
            //进行文件上传
//            String file = uploadFile(filePath);
//            AoaAttachmentListOfl aoaAttachment = new AoaAttachmentListOfl();
//            aoaAttachment.setAttachmentPath(file);
//            aoaAttachment.setAttachmentName(file);
//            int document = aoaPlanListService.insertSelectivePath(aoaAttachment);
//            Long attachId = aoaAttachment.getAttachmentId();

            //进行计划修改
            int i = aoaPlanListService.updateByPrimaryKeySelective(aoaPlanList);
            re="forward:planview?pageNum="+pageNum;
        }
        return re;
    }
    public String uploadFile(MultipartFile file){
        String uploadUrl = null;
        try {

            if(null != file){
                String filename = file.getOriginalFilename();
                if(!"".equals(filename.trim())){
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    //上传到OSS
                    uploadUrl = constantProperties.getHead() + aliyunOSSUtil.upload(newFile) + constantProperties.getEnd();

                }

            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return uploadUrl;
    }
}
